<template>
  <div class="app-container">
    <el-form :inline="true" class="demo-form-inline">
      <el-form-item label="题库名称">
        <el-input
          clearable
          @clear="search()"
          v-model="searchMap.where.title"
          placeholder="题库名称"
        ></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="search()">查询</el-button>
        <el-button type="primary" @click="form={};dialogFormVisible = true"
          >添加题库</el-button
        >
        <el-button type="danger" @click="deleteBatch()">批量删除</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="repoList" style="width: 100%" @select="selectItem">
      <el-table-column
      type="selection"
      width="55">
    </el-table-column>
      <el-table-column prop="title" label="题库名称" width="180">
      </el-table-column>
      <el-table-column prop="remark" label="题库备注"> </el-table-column>
      <el-table-column prop="radioCount" label="单选数量"></el-table-column>
      <el-table-column prop="multiCount" label="多选数量"> </el-table-column>
      <el-table-column prop="judgeCount" label="判断数量"> </el-table-column>
      <el-table-column prop="jdtCount" label="简答数量"> </el-table-column>
      <el-table-column prop="createTime" label="创建时间"> </el-table-column>
      <el-table-column label="操作" width="180">
        <template slot-scope="scope">
       
          <el-button type="primary" size="mini" icon="el-icon-edit" @click="form=scope.row;dialogFormVisible=true"
            >修改</el-button
          >
          <el-button
            type="danger"
            size="mini"
            icon="el-icon-delete"
            @click="deleteById(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <br />
    <el-pagination
      style="float: right"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="searchMap.now"
      :page-sizes="[5, 10, 50, 100]"
      :page-size="searchMap.size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
    <el-dialog title="添加/修改题库" :visible.sync="dialogFormVisible">
      <el-form ref="repo" :model="form" :rules="rules">
        <el-form-item label="题库名称" prop="title">
          <el-input v-model="form.title" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="题库描述">
          <el-input v-model="form.remark" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button
          type="primary"
          @click="
            save('repo');
            dialogFormVisible = false;
          "
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { fetchData, deleteById,save,deleteBatch  } from "@/api/repo.js";
export default {
  data() {
      //自定义规则
    var checkTitle=(rule,value,callback)=>{
      if (value.indexOf("@")==-1) {
        return callback(new Error('标题必须包含@'));
      }else{
        callback()
      }
    };
    return {
      repoList: [],
      searchMap: {
        now: 1,
        size: 5,
        where: {
          title: "",
        },
      },
      total: 0,
      dialogFormVisible: false,
      form: {
      
      },
      rules: {
        title: [
          { required: true, message: "请输入题库名称", trigger: "blur" },
          { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" },
          { validator: checkTitle, trigger: "blur" },
        ],
      },
      deleteIds:[],
    };
  },
  methods: {
    search() {
      fetchData(this.searchMap).then((res) => {
        console.log(res);
        this.repoList = res.list;
        this.total = res.total;
      });
    },
    save(formName) {
      //表单校验
      this.$refs[formName].validate((valid) => {
        if (valid) {
          
          save(this.form).then((res) => {
            this.$message({
              message: res.msg,
              type: res.code == 0 ? "success" : "danger",
            });
            this.dialogFormVisible = false;
            this.searchMap.now = 1;
            this.search();
          });
        } else {
          return false;
        }
      });
    },
    handleSizeChange(size) {
      this.searchMap.size = size;
      this.search();
    },
    handleCurrentChange(page) {
      this.searchMap.now = page;
      this.search();
    },

    deleteById(id) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          deleteById(id).then((res) => {
            this.$message({
              type: res.code == 0 ? "success" : "danger",
              message: res.msg,
            });
            this.search();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除1",
          });
        });
    },
    deleteBatch(){
      deleteBatch(this.deleteIds).then(res=>{
        this.$message({
          type: res.code==0?'success':'danger',
          message: res.msg
        });
        this.search();
      })
    },
    selectItem(selection,row){
      console.log(selection);
      this.deleteIds=[];
      selection.forEach(item=>{
        this.deleteIds.push(item.id);
      })
    }
  },
  created() {
    this.search();
  },
};
</script>